<template>
  <div class="app-container">
    <!-- 查询商品 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodName">
        <el-input v-model="queryParams.goodName" placeholder="请输入商品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品进价" prop="purchPrice">
        <el-input v-model="queryParams.purchPrice" placeholder="请输入商品进价" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品描述" prop="goodinfo">
        <el-input v-model="queryParams.goodinfo" placeholder="请输入商品描述" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品类别" prop="typeId">
        <el-select v-model="queryParams.typeId" placeholder="请选择商品类别" clearable>
          <el-option v-for="dict in dict.type.goods_type" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-check" v-hasPermi="['system:purchase:add']" type="success" size="mini"
          @click="handleAdd">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品id" align="center" prop="goodID" />
      <el-table-column label="商品名称" align="center" prop="goodName" />
      <el-table-column label="商品进价" align="center" prop="purchPrice" />
      <el-table-column label="商品照片" align="center">
        <template slot-scope="scope">
          <el-image style="width: 100px; height: 100px" :src="scope.row.goodImg" fit="fill"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="商品类别" align="center" prop="typeId">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.goods_type" :value="scope.row.typeId" />
        </template>
      </el-table-column>
      <el-table-column label="选择个数" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-input-number size="small" v-model="goodsCount[scope.row.goodID]" :min="0"></el-input-number>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { listGoods } from "@/api/system/goods";
import { addPurchasedetails } from "@/api/system/purchasedetails"
export default {
  name: "Purchasedetails",
  dicts: ['goods_type'],
  data() {
    return {
      goodsCount: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 商品列表
      goodsList: [],
      // 总条数
      total: 0,
      // 采购单详情表格数据
      purchasedetailsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        purchaseDetailsID:null,
        pageNum: 1,
        pageSize: 10,
        goodID: null,
        goodNum: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    var purchaseID = this.$route.params && this.$route.params.purchaseID;
    this.getList();
    this.form.purchaseDetailsID = purchaseID;
  },
  methods: {
    /** 查询商品列表 */
    getList() {
      var that = this;
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).then(
        () => {
          for (var i = 0; i < this.goodsList.length; i++) {
            that.goodsCount[that.goodsList[i].goodID] = 0;
          }
        }
      )
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        purchaseDetailsID: null,
        goodID: null,
        goodNum: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.purchaseDetailsID)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form.data = this.goodsCount;
      this.submitForm();
    },

    /** 提交按钮 */
    submitForm() {
      addPurchasedetails(this.form).then(response => {
        if (response.code === 200) {
          this.$modal.msgSuccess("新增成功");
          this.$tab.closeOpenPage({
            path: "/purchase"
          });
        }
        else {
          this.$modal.msgError("新增失败");
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const purchaseDetailsIDs = row.purchaseDetailsID || this.ids;
      this.$modal.confirm('是否确认删除采购单详情编号为"' + purchaseDetailsIDs + '"的数据项？').then(function () {
        return delPurchasedetails(purchaseDetailsIDs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/purchasedetails/export', {
        ...this.queryParams
      }, `purchasedetails_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
